package com.fr.report.cellElement;

import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.report.core.ScriptUtils;
import com.fr.report.script.Calculator;
import com.fr.report.script.CurrentValueNameSpace;
import com.fr.report.script.core.parser.UtilEvalError;
import java.util.logging.Level;
import java.util.logging.Logger;

public class FormulaPresent extends AbstractPresent
{
  private String formulaContent = "";

  public FormulaPresent()
  {
  }

  public FormulaPresent(String paramString)
  {
    setFormulaContent(paramString);
  }

  public String getFormulaContent()
  {
    return this.formulaContent;
  }

  public void setFormulaContent(String paramString)
  {
    if (paramString == null)
      paramString = "";
    this.formulaContent = paramString;
  }

  public String[] getDependence()
  {
    return ScriptUtils.getDependenceByFormula(this.formulaContent);
  }

  public Object present(Object paramObject, Calculator paramCalculator)
  {
    if ((StringUtils.isBlank(this.formulaContent)) || (paramObject == null))
      return super.present(paramObject, paramCalculator);
    if (paramObject instanceof Formula)
      paramObject = ((Formula)paramObject).getResult();
    CurrentValueNameSpace localCurrentValueNameSpace = new CurrentValueNameSpace(paramObject);
    paramCalculator.pushNameSpace(localCurrentValueNameSpace);
    try
    {
      Object localObject1 = paramCalculator.eval((this.formulaContent.startsWith("=")) ? this.formulaContent.substring(1) : this.formulaContent);
      return localObject1;
    }
    catch (UtilEvalError localUtilEvalError)
    {
      FRContext.getLogger().log(Level.WARNING, localUtilEvalError.getMessage(), localUtilEvalError);
    }
    finally
    {
      paramCalculator.removeNameSpace(localCurrentValueNameSpace);
    }
    return paramObject;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if ((paramXMLableReader.isChildNode()) && ("Content".equals(paramXMLableReader.getTagName())))
    {
      String str = paramXMLableReader.getElementValue();
      if (str != null)
        this.formulaContent = str;
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    if (StringUtils.isNotBlank(this.formulaContent))
      paramXMLPrintWriter.startTAG("Content").textNode(getFormulaContent()).end();
  }

  public boolean equals(Object paramObject)
  {
    return ((paramObject instanceof FormulaPresent) && (ComparatorUtils.equals(this.formulaContent, ((FormulaPresent)paramObject).formulaContent)));
  }
}